﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Reflection;

namespace DirectorySynchronizer
{
    internal enum State { OptionsSetup, Synchronizing, SynchronizationCanceled, SynchronizationCompleted }
    static class Program
    {
        internal static readonly string VersionNumber = "1.0";
        internal static readonly string OperantNamespace = "[On-Space Software Inc.]http://on-space-si.com/-ds-v-" + VersionNumber.Replace('.','p') + "-[nsdaii]";
        internal static string LicensedNamespace = null;
        private const int writewaittimeout = 100;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] argStrings)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (Environment.OSVersion.Version.Major == 6)
            {
                LicensedNamespace = Assembly.GetExecutingAssembly().GetTypes()[0].Namespace;
                if (!OnSpaceSI.LicenseFiler.LicenseAcceptancePresent(LicensedNamespace))
                {
                    if (!ShowLicenseDialogAndSaveAccept())
                    {
                        MessageBox.Show("Unable to save license acceptance, or license not accepted, exiting program.", "Directory Synchronizer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        Environment.Exit(0);
                    }
                }
            }
            else
            {
                string message = "This software program may not be compatible with this version of Microsoft Windows,\r\ncheck the on-space-si.com website to see if there is an update.";
                MessageBox.Show(message, "Directory Synchronizer: Unexpected Microsoft Windows Version", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Environment.Exit(0);
            }


            if (argStrings.Length == 1)
            {
                if (argStrings[0].EndsWith(DirSyncOptions.OptsFileExt))
                {
                    if (File.Exists(argStrings[0]))
                    {
                        Application.Run(new MainForm(DirSyncOptions.LoadOptionsFile(argStrings[0])));
                        return;
                    }
                }
            }
            else if (argStrings.Length == 2)
            {
                if (Directory.Exists(argStrings[0]) && Directory.Exists(argStrings[1]))
                {
                    Application.Run(new MainForm(argStrings[0], argStrings[1]));
                    return;
                }
            }
            Application.Run(new MainForm());
        }
        private static bool ShowLicenseDialogAndSaveAccept()
        {
            OnSpaceSI.LicenseDisplayForm ldf = new OnSpaceSI.LicenseDisplayForm();
            Application.Run(ldf);
            if (ldf.DialogResult == DialogResult.OK)
            {
                return OnSpaceSI.LicenseFiler.SaveLicenseAcceptance(LicensedNamespace);
            }
            return false;
        }
    }

    public interface ICancelable
    {
        void Cancel();
        bool Cancelled { get; }
    }

    public interface IProgressDisplay
    {
        void UpdateProgress(string argformattedupdatetext, long argprogress, long argmax);
        void ProcessFinished();
        bool Disposing { get; }
        bool IsDisposed { get; }
    }

    public delegate void OneStringParameterCrossThreadDelegate(string argstring);
    public delegate void ProgressUpdateCrossThreadDelegate(string argformattedupdatetext, long argprogresslong, long argmaxlong);
}
